package com.ruoyi.asms.util;

import com.ruoyi.asms.domain.vo.InspectionReportVo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.util.IOUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 巡检结果报表
 */
public class InspectionExcelUtil {
    public void exportInspectionExcel(HttpServletResponse response, InspectionReportVo model, String sheetName)throws IOException{
        HSSFWorkbook workbook = new HSSFWorkbook();
       // workbook.setSheetName(0, sheetName);
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setWrapText(true);
        HSSFSheet sheet = workbook.createSheet("sheet");
        sheet.setColumnWidth(0,13*256);
        sheet.setColumnWidth(1,26*256);
        sheet.setColumnWidth(2,24*256);
        sheet.setColumnWidth(3,19*256);
        sheet.setColumnWidth(4,7*256);
        sheet.setColumnWidth(5,18*256);


        int rownum = 0;

        HSSFRow row0 = sheet.createRow(rownum);
        HSSFCell cell_00 = row0.createCell(0);
        HSSFFont font = workbook.createFont();
        font.setFontName("等线");
        font.setFontHeightInPoints((short) 18);// 设置字体大小
        HSSFCellStyle style1 = workbook.createCellStyle();
        style1.setAlignment(HorizontalAlignment.CENTER);
        style1.setVerticalAlignment(VerticalAlignment.CENTER);
        style1.setFont(font);
        cell_00.setCellStyle(style1);
        cell_00.setCellValue(model.getTitle());

        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 0, 5));

        HSSFFont font1 = workbook.createFont();
        font1.setFontName("等线");
        font1.setFontHeightInPoints((short) 11);// 设置字体大小
        style.setFont(font1);
        rownum++;
        HSSFRow row1 = sheet.createRow(rownum);
        row1.setHeight((short)400);
        HSSFCell cell_10 = row1.createCell(0);
        cell_10.setCellStyle(style);
        cell_10.setCellValue("客户名称");

        HSSFCell cell_11 = row1.createCell(1);
        cell_11.setCellStyle(style);
        cell_11.setCellValue(model.getCustomerDeptName());

        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 1, 5));

        rownum++;
        HSSFRow row2 = sheet.createRow( rownum);
        HSSFCell cell_20 = row2.createCell(0);
        row2.setHeight((short)400);
        cell_20.setCellStyle(style);
        cell_20.setCellValue("巡检单位");

        HSSFCell cell_21 = row2.createCell(1);
        cell_21.setCellStyle(style);
        cell_21.setCellValue(model.getInspectionDeptName());

        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 1, 5));

        rownum++;
        HSSFRow row3 = sheet.createRow( rownum);
        row3.setHeight((short)400);
        HSSFCell cell_30 = row3.createCell(0);
        cell_30.setCellStyle(style);
        cell_30.setCellValue("巡检时间");

        HSSFCell cell_31 = row3.createCell(1);
        cell_31.setCellStyle(style);
        cell_31.setCellValue(model.getInspectionTime());

        HSSFCell cell_32 = row3.createCell(2);
        cell_32.setCellStyle(style);
        cell_32.setCellValue("巡检人员");

        HSSFCell cell_33 = row3.createCell(3);
        cell_33.setCellStyle(style);
        cell_33.setCellValue(model.getInspectionUserName());

        HSSFCell cell_34 = row3.createCell(4);
        cell_34.setCellStyle(style);
        cell_34.setCellValue("电话");

        HSSFCell cell_35 = row3.createCell(5);
        cell_35.setCellStyle(style);
        cell_35.setCellValue(model.getInspectionUserPhone());

        rownum++;
        HSSFRow row4 = sheet.createRow( rownum);
        row4.setHeight((short)400);
        HSSFCell cell_40 = row4.createCell(0);
        cell_40.setCellStyle(style);
        cell_40.setCellValue("巡检内容");

        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 0, 5));

        rownum++;
        HSSFRow row5 = sheet.createRow( rownum);
        row5.setHeight((short)400);
        HSSFCell cell_50 = row5.createCell(0);
        cell_50.setCellStyle(style);
        cell_50.setCellValue("序号");

        HSSFCell cell_51 = row5.createCell(1);
        cell_51.setCellStyle(style);
        cell_51.setCellValue("设备名称");

        HSSFCell cell_52 = row5.createCell(2);
        cell_52.setCellStyle(style);
        cell_52.setCellValue("总数");

        HSSFCell cell_53 = row5.createCell(3);
        cell_53.setCellStyle(style);
        cell_53.setCellValue("正常数量");

        HSSFCell cell_55 = row5.createCell(5);
        cell_55.setCellStyle(style);
        cell_55.setCellValue("异常数量");

        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 3, 4));


        List<InspectionReportVo.InspectionResult> contents = model.getContent();

        for(int i = 0;contents != null && i<contents.size();i++){
            rownum++;
            HSSFRow rowN = sheet.createRow(rownum);
            rowN.setHeight((short)400);
            HSSFCell cell_N0 = rowN.createCell(0);
            cell_N0.setCellStyle(style);
            cell_N0.setCellValue(i+1);

            HSSFCell cell_N1 = rowN.createCell(1);
            cell_N1.setCellStyle(style);
            cell_N1.setCellValue(contents.get(i).getAssetTypeName());

            HSSFCell cell_N2 = rowN.createCell(2);
            cell_N2.setCellStyle(style);
            cell_N2.setCellValue(contents.get(i).getTotal());

            HSSFCell cell_N3 = rowN.createCell(3);
            cell_N3.setCellStyle(style);
            cell_N3.setCellValue(contents.get(i).getNormalNumber());

            HSSFCell cell_N5 = rowN.createCell(5);
            cell_N5.setCellStyle(style);
            cell_N5.setCellValue(contents.get(i).getUnNormalNumber());

            sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 3, 4));

        }

        rownum++;
        int length = model.getSummary().split("\n").length;
        HSSFRow rowA = sheet.createRow(rownum);
        int high = (length+3)*300;
        rowA.setHeight((short)high);
        HSSFCell cell_A0 = rowA.createCell(0);
        cell_A0.setCellStyle(style);
        cell_A0.setCellValue("巡检结果及处理意见");

        HSSFCellStyle style3 = workbook.createCellStyle();
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style3.setWrapText(true);
        style3.setFont(font1);

        HSSFCell cell_A2 = rowA.createCell(2);
        cell_A2.setCellStyle(style3);
        cell_A2.setCellValue(model.getSummary());

        HSSFCell cell_A4 = rowA.createCell(4);
        cell_A4.setCellStyle(style3);
        cell_A4.setCellValue("巡检人员签字：");

        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 0, 1));
        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 2, 3));
        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 4, 5));

        rownum++;
        HSSFRow rowB = sheet.createRow(rownum);
        rowB.setHeight((short)1800);
        HSSFCell cell_B0 = rowB.createCell(0);
        cell_B0.setCellStyle(style);
        cell_B0.setCellValue("客户意见");


        HSSFCell cell_B4 = rowB.createCell(4);
        cell_B4.setCellStyle(style);
        cell_B4.setCellValue("客户签字（盖章）");


        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 0, 1));
        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 2, 3));
        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 4, 5));

        rownum++;
        HSSFRow rowC = sheet.createRow(rownum);
        HSSFCell cell_C0 = rowC.createCell(0);
        cell_C0.setCellStyle(style);
        cell_C0.setCellValue("备注");

        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 0, 1));
        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 2, 5));

        CellRangeAddress cra = new CellRangeAddress(0, rownum, 0, 5);
        RegionUtil.setBorderBottom(BorderStyle.THICK, cra, sheet); // 下边框

        RegionUtil.setBorderLeft(BorderStyle.THICK, cra, sheet); // 左边框

        RegionUtil.setBorderRight(BorderStyle.THICK, cra, sheet); // 有边框

        RegionUtil.setBorderTop(BorderStyle.THICK, cra, sheet); // 上边框

        ServletOutputStream outputStream = response.getOutputStream();
        try
        {
            workbook.write(outputStream);
        }
        catch (Exception e)
        {
            e.printStackTrace();
          //  log.error("导出Excel异常{}", e.getMessage());
        }
        finally
        {
            IOUtils.closeQuietly(workbook);
            IOUtils.closeQuietly(outputStream);
        }
    }
}
